<template>
    <div class="wrapper">
        <!--header部分-->
        <header>
            <p>地址管理</p>
        </header>

        <!--地址列表部分-->
        <ul class="addresslist">
            <li v-for="item in deliveryAddressArr">
                <div class="left" @click="setDeliveryAddress(item)">
                    <h3>{{ item.contactName }} {{ item.contactSex | sexFilter }} {{ item.contactTel }}</h3>
                    <p>{{ item.address }}</p>
                </div>
                <div class="right">
                    <i class="fa fa-edit" @click="editUserAddress(item.daId)"></i>
                    <i class="fa fa-remove" @click="removeUserAddress(item.daId)"></i>
                </div>
            </li>
        </ul>

        <!--新增地址部分-->
        <div class="addbtn" @click="toAddUserAddress">
            <i class="fa fa-plus-circle"></i>
            <p>新增收货地址</p>
        </div>

        <!--底部菜单-->
        <Footer></Footer>
    </div>
</template>

<script>
import Footer from "@/components/Footer";
import {useRoute, useRouter} from "vue-router";

let route, router

export default {
    name: "UserAddress",
    data() {
        route = useRoute()
        return {
            fromOrder: route.query.fromOrder,
            user: {},
            deliveryAddressArr: []
        }
    },
    created() {
        router = useRouter()
        this.user = this.$getSessionStorage('user');
        this.listDeliveryAddressByUserId();
    },
    components: {
        Footer
    },
    filters: {
        sexFilter(value) {
            return value === 1 ? '先生' : '女士';
        }
    },
    methods: {
        //列出用户的所有地址
        listDeliveryAddressByUserId() {
            //查询送货地址
            this.$axios.get('deliveryaddress', {
                params: {
                    userId: this.user.userId
                }
            }).then(response => {
                this.deliveryAddressArr = response.data.data;
            }).catch(error => {
                console.error(error);
            });
        },
        setDeliveryAddress(deliveryAddress) {
            //把用户选择的默认收货地址存储到LocalStorage中
            this.$setLocalStorage('defaultaddress', deliveryAddress);
            //businessId=-1代表从个人中心的我的地址进入，这个时候不应该有跳转到订单的动作
            if (this.fromOrder) router.push({name: 'HomePage'});
            //跳转到订单
            else router.push({path: '/orders', query: {businessId: this.businessId}});
        },
        //跳转到addUserAddress
        toAddUserAddress() {
            router.push({path: '/addUserAddress', query: {businessId: this.businessId}});
        },
        //跳转到editUserAddress
        editUserAddress(daId) {
            router.push({path: '/editUserAddress', query: {businessId: this.businessId, daId: daId}});
        },
        //删除送货地址
        removeUserAddress(daId) {
            if (!confirm('确认要删除此送货地址吗?')) {
                return;
            }
            let url = `deliveryaddress/${daId}`;
            this.$axios.delete(url).then(response => {
                if (response.data.code === 200) {
                    //删除成功且删除的页面就是之前存储的默认地址，要把存储的默认地址删除
                    let deliveryAddress = this.$getLocalStorage(this.user.userId);
                    if (deliveryAddress != null && deliveryAddress.daId === daId) {
                        this.$removeLocalStorage(this.user.userId);
                    }
                    //重新列出地址
                    this.listDeliveryAddressByUserId();
                } else {
                    alert('删除失败!');
                }
            }).catch(error => {
                console.error(error);
            });
        }
    }
}
</script>

<style scoped>
/**************************总容器**************************/
.wrapper {
    width: 100%;
    height: 100%;
    background-color: #F5F5F5;
}

/**************************header**************************/
.wrapper header {
    width: 100%;
    height: 12vw;
    background-color: #0097ff;
    display: flex;
    align-items: center;
    justify-content: space-around;
    color: #fff;
    font-size: 4.8vw;
    position: fixed;
    left: 0;
    top: 0;
    /*保证在最上层*/
    z-index: 1000;
}

/**************************addressList**************************/
.wrapper .addresslist {
    width: 100%;
    margin-top: 12vw;
    background-color: #fff;
}

.wrapper .addresslist li {
    width: 100%;
    box-sizing: border-box;
    border-bottom: solid 1px #DDD;
    padding: 3vw;

    display: flex;
}

.wrapper .addresslist li .left {
    flex: 5;
    /*左边这块区域是可以点击的*/
    user-select: none;
    cursor: pointer;
}

.wrapper .addresslist li .left h3 {
    font-size: 4.6vw;
    font-weight: 300;
    color: #666;
}

.wrapper .addresslist li .left p {
    font-size: 4vw;
    color: #666;
}

.wrapper .addresslist li .right {
    flex: 1;
    font-size: 5.6vw;
    color: #999;
    cursor: pointer;

    display: flex;
    justify-content: space-around;
    align-items: center;
}

/**************************新增地址**************************/
.wrapper .addbtn {
    width: 100%;
    height: 14vw;
    border-top: solid 1px #DDD;
    border-bottom: solid 1px #DDD;
    background-color: #fff;
    margin-top: 4vw;

    display: flex;
    justify-content: center;
    align-items: center;

    font-size: 4.5vw;
    color: #0097FF;
    user-select: none;
    cursor: pointer;
}

.wrapper .addbtn p {
    margin-left: 2vw;
}
</style>